﻿@{
    ViewBag.Title = "商品管理";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script>
    $(function() {
     
        gridList();
    });
    function gridList() {
        var $gridList = $("#gridList");
        var postData = {
            keyword: ""
        };
        $gridList.dataGrid({
            url: "/ProductManage/Product/GetGridJson",
            postData: postData,
            height: $(window).height() - 128,
            multiselect: true,
            colModel: [
                { label: '主键', name: 'F_Id', hidden: true },
                { label: '商品名称', name: 'F_ProductName', width: 300, align: 'left' },
                { label: '商品编码', name: 'F_ProductCode', width: 150, align: 'left' },
                 {
                     label: '库存', name: 'F_Stock', width: 80, align: 'center'
                 },
                { label: '销售价(元)', name: 'F_Price', width: 80, align: 'center' ,
                formatter: function (cellvalue, options, rowObject) {
                    return cellvalue == null ? "" : parseFloat(cellvalue).toFixed(2);
                        }},
                {
                    label: '成本价(元)', name: 'F_LowestPrice', width: 60, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        return cellvalue == null ? "" : parseFloat(cellvalue).toFixed(2);
                    }
                },
                {
                    label: '销售数量', name: 'F_SaleCounts', width: 80, align: 'center'
                },
                {
                    label: '访问次数', name: 'F_VisitCounts', width: 80, align: 'center'
                },

                 {
                     label: '是否上架', name: 'F_EnabledMark', width: 80, align: 'center',
                     formatter: function (cellvalue) {
                         return cellvalue ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                     }
                 },
                 {
                     label: '标准产品', name: 'F_Promotion', width: 80, align: 'center',
                     formatter: function (cellvalue) {
                         return cellvalue ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                     }
                 },
                 { label: '促销价(元)', name: 'F_PromotionPrice', width: 80, align: 'center' ,
                     formatter: function (cellvalue, options, rowObject) {
                         return cellvalue == null ? "" : parseFloat(cellvalue).toFixed(2);
                     }
                 },
                {
                    label: '发布时间', name: 'F_CreatorTime', width: 80, align: 'left',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' }
                }
            ],
            pager: "#gridPager",
            sortname: 'F_CreatorTime desc',
            viewrecords: true
        });
        $("#btn_search").click(function () {
            $gridList.jqGrid('setGridParam', {
                postData: { keyword: $("#txt_keyword").val() }
            }).trigger('reloadGrid');
        });
    }
    function btn_add() {
        $.modalOpen({
            id: "Form",
            title: "添加商品",
            url: "/ProductManage/Product/Form",
            width: "950px",
            height: "710px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_edit() {
        var rows = $("#gridList").jqGridRowValue();
        if (rows.length > 0) {
            var keyValue = rows[rows.length - 1].F_Id;
            $.modalOpen({
                id: "Form",
                title: "修改商品",
                url: "/ProductManage/Product/Form?keyValue=" + keyValue,
                width: "950px",
                height: "710px",
                callBack: function (iframeId) {
                    top.frames[iframeId].submitForm();
                }
            });
        }else{
             $.modalAlert("请先选择需要修改的商品！", "error");
        }
    }
    function btn_delete() {
        var rows = $("#gridList").jqGridRowValue();
        var ids = $.map(rows, function (item, idx) {
            return item.F_Id;
        }).join(',');
        if(ids.length>0){
            $.deleteForm({
                prompt: "注：您确定要【删除】选定项商品吗？",
                url: "/ProductManage/Product/DeleteForm",
                param: { keyValues: ids },
                success: function () {
                  $("#gridList").trigger("reloadGrid");
                   $(".toolbar .operate").find('.close').click();
                }
            });}else{
             $.modalAlert("请先选择需要删除的商品！", "error");
        }
    }
    function btn_details() {
        var rows = $("#gridList").jqGridRowValue();
        if (rows.length > 0) {
            var keyValue = rows[rows.length - 1].F_Id;
            $.modalOpen({
                id: "Details",
                title: "查看商品",
                url: "/ProductManage/Product/Details?keyValue=" + keyValue,
                width: "950px",
                height: "710px",
                btn: null
            });
        }else{
            $.modalAlert("请先选择需要查看的商品！", "error");
        }
    }
    function btn_outStock() {
        var ids = getSelectIds();
        if(ids.length>0){
            $.modalConfirm("注：您确定要【下架】选定商品吗？", function (r) {
                if (r) {
                    $.submitForm({
                        url: "/ProductManage/Product/InStock",
                        param: { keyValue: ids, flag:0 },
                        success: function () {
                           $("#gridList").trigger("reloadGrid");
                           $(".toolbar .operate").find('.close').click();
                        }
                    })
                }
            });}else{
            $.modalAlert("请先选择需要下架的商品！", "error");
        }
    }
    function btn_InStock() {
        var ids = getSelectIds();
        if(ids.length>0){
            $.modalConfirm("注：您确定要【上架】选定商品吗？", function (r) {
                if (r) {
                    $.submitForm({
                        url: "/ProductManage/Product/InStock",
                        param: { keyValue: ids, flag: 1 },
                        success: function () {
                            $("#gridList").trigger("reloadGrid");
                           $(".toolbar .operate").find('.close').click();
                        }
                    })
                }
            });
        }else{
            $.modalAlert("请先选择需要上架的商品！", "error");
        }
    }
    function btn_audit() {
        var ids = getSelectIds();
        if(ids.length>0){
            $.modalConfirm("注：您确定要【审核通过】选定商品吗？", function (r) {
                if (r) {
                    $.submitForm({
                        url: "/ProductManage/Product/auditProduct",
                        param: { keyValue: ids },
                        success: function () {
                            $("#gridList").trigger("reloadGrid");
                            $(".toolbar .operate").find('.close').click();
                        }
                    })
                }
            });
        }else{
            $.modalAlert("请先选择需要审核的商品！", "error");
        }
    }
    function getSelectIds() {
        var rows = $("#gridList").jqGridRowValue();
        var ids = $.map(rows, function (item, idx) {
            return  "'" + item.F_Id + "'" ;
        }).join(',');
        return ids;
    }
</script>

<div class="topPanel">
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control" placeholder="商品名称/商品编码" style="width: 200px;">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
        </div>
        <div class="btn-group">
            <a id="NF-add" authorize="yes" class="btn btn-primary dropdown-text" onclick="btn_add()"><i class="fa fa-plus"></i>添加商品</a>
        </div>
        <div class="operate">
            <ul class="nav nav-pills">
                <li class="first">已选中<span>1</span>项</li>
                <li><a id="NF-edit" authorize="yes" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>修改商品</a></li>
                <li><a id="NF-delete" authorize="yes" onclick="btn_delete()"><i class="fa fa-trash-o"></i>删除商品</a></li>
                <li><a id="NF-Details" authorize="yes" onclick="btn_details()"><i class="fa fa-search-plus"></i>查看商品</a></li>
                <li class="split"></li>
                <li><a id="NF-InStock" onclick="btn_InStock()"><i class="fa fa-play-circle"></i>批量上架</a></li>
                <li><a id="NF-outStock" onclick="btn_outStock()"><i class="fa fa-stop-circle"></i>批量下架</a></li>
            </ul>
            <a href="javascript:;" class="close"></a>
        </div>
        <!-- <script>$('.toolbar').authorizeButton()</script>-->
    </div>

</div>
<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>
